<?php
/**
 * Config
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use framework\library\database\DB;
/**
 * Config Class
 *
 * Handles configuration for Plurious Framework
 * 
 * Allows the use of DB configuration setup as well as a file based configuration
 * setup.
 *
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\library\database\DB
 */
class Config {

    /** Holds the active configuration */
    private $data = array('loaded'=>array());

    /**
     * Loads the default configuration
     */
    public function __construct() {
        if (empty($data)) {
            $this->loadAppConfig(0);
        }
    }

    /**
     * Return a value that corresponds to the given key
     * 
     * @param string $key the value corresponding to this key will be returned
     * @return mixed value corresponding the provided key
     */
    public function get($key) {
        return (isset($this->data[$key]) ? $this->data[$key] : null);
    }

    /**
     * Store the key/value pair in the registry
     * 
     * @param string $key
     * @param mixed $value 
     */
    public function set($key, $value) {
        $this->data[$key] = $value;
    }

    /**
     * Check for a value in the registry that corresponds to the given key
     * 
     * @param string $key to search for
     * @return boolean True if value exists for the given key
     */
    public function has($key) {
        return isset($this->data[$key]);
    }

    /**
     * Loads configuration information from a properly constructed config file.
     * 
     * File must contain only variable assigments that assign key/value pairs 
     * to elements of an array named $cfg like so:
     * 
     * $cfg['key'] = 'value';
     * 
     * @param string $filename 
     */
    public function load($file) {
        $filename = basename($file);
        // Check if config was already loaded
        $loaded = $this->get('loaded');
        if (in_array($filename, $loaded))
            return;

        $loaded[] = $filename;
        $this->set('loaded', $loaded);
        $file = $file . '.php';
        if (is_file($file)) {
            $cfg = array();
            require_once $file;
            $this->data = array_merge($this->data, $cfg);
        } else {
            throw new ConfigException('Error: Could not load config ' . $filename . '!');
            // @codeCoverageIgnoreStart
            if(!PHPUNIT){
                exit();
            }
            // @codeCoverageIgnoreEnd
        }
    }

    /**
     * Loads configuration data from the database for a given Application or 
     * context.
     * 
     * @param int $app_id Indicates the context to load configuration data for
     */
    private function loadAppConfig($app_id) {
        $this->set('loaded', array('app'));
        $this->db = DB::getInstance(DB_DRIVER, DB_HOSTNAME, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
        $this->db->setOrder('app_id', 'DESC');
        $result = $this->db->select('setting', array('app_id' => $app_id));

        foreach ($result as $setting) {
            if (!$setting['serialized']) {
                $this->set($setting['name'], $setting['value']);
            } else {
                $this->set($setting['name'], unserialize($setting['value']));
            }
        }
    }

}

/**
 * ConfigException Class
 *
 * Handles exceptios from the configuration class
 *
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\library\database\DB
 * @uses \Exception
 */
class ConfigException extends \Exception {
    public function __construct($e){
        
        echo $e;
    }
}
?>